package com.hinnotek.amoeba.ui.activity.old

import android.os.Bundle
import android.view.ViewGroup
import android.widget.RadioGroup
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentPagerAdapter
import androidx.viewpager.widget.ViewPager
import com.lee.common.base.BaseActivity
import com.hinnotek.amoeba.R
import com.hinnotek.amoeba.databinding.ActivityStatisticalBinding
import com.hinnotek.amoeba.ui.fragment.FragmentStatisticalCustomerProject
import com.hinnotek.amoeba.ui.fragment.FragmentStatisticalRDProject
import com.hinnotek.amoeba.widget.ITitleBarListener
import com.hinnotek.amoeba.widget.MyTitleBar
import dagger.hilt.android.AndroidEntryPoint
import java.util.*

@AndroidEntryPoint
class StatisticalActivity : BaseActivity<ActivityStatisticalBinding>(), ViewPager.OnPageChangeListener, ITitleBarListener {

    private var fragments: MutableList<Fragment> = mutableListOf()
    private lateinit var mTitleBar: MyTitleBar
    private var fragmentStatisticalCustomerProject: FragmentStatisticalCustomerProject? = null
    private var rdProject: FragmentStatisticalRDProject? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_statistical)
    }

    override fun initVew() {

        val orgId: Int = intent.getIntExtra("orgId", -1)
        mTitleBar = MyTitleBar(this)
        mTitleBar.setTitleBarListener(this)
        mTitleBar.setLeftVisible(false)
        mTitleBar.setRightVisible(false)

        bindingView.llMainActivityHome.addView(mTitleBar, 0)

        fragments = ArrayList()
        fragments.add(FragmentStatisticalCustomerProject().also {
            fragmentStatisticalCustomerProject = it
        })
        fragments.add(FragmentStatisticalRDProject().also { rdProject = it })
        fragmentStatisticalCustomerProject?.setOrgId(orgId)
        rdProject?.setOrgId(orgId)
        bindingView.flHome.adapter = object : FragmentPagerAdapter(this.supportFragmentManager) {
            override fun getCount(): Int {
                return fragments.size
            }

            override fun getItem(position: Int): Fragment {
                return fragments.get(position)
            }


        }
        bindingView.flHome.setOnPageChangeListener(this)
        bindingView.homeTopBar.check(R.id.home_customer_project)
        bindingView.flHome.currentItem = 0
        mTitleBar.setTitleValue("客户项目")
        bindingView.homeTopBar.setOnCheckedChangeListener(object : RadioGroup.OnCheckedChangeListener {
            override fun onCheckedChanged(group: RadioGroup, checkedId: Int) {
                when (checkedId) {
                    R.id.home_customer_project -> {
                        bindingView.flHome.currentItem = 0
                        mTitleBar.setTitleValue("客户项目")
                    }
                    R.id.home_rd_project -> {
                        bindingView.flHome.currentItem = 1
                        mTitleBar.setTitleValue("研发项目")
                    }
                }
            }
        })
    }

    override fun setData() {

    }



    override fun onResume() {
        super.onResume()
    }

    override fun finish() {
        val view: ViewGroup = getWindow().getDecorView() as ViewGroup
        view.removeAllViews()
        super.finish()
    }


    override fun onPageScrollStateChanged(state: Int) {}


    override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {}

    override fun onPageSelected(position: Int) {
        when (fragments.size) {
            0 -> bindingView.homeTopBar.check(R.id.home_customer_project)
            1 -> bindingView.homeTopBar.check(R.id.home_rd_project)
            else -> {
            }
        }
    }

    override fun onLeftLayoutListener() {}
    override fun onRightLayoutListener() {}

}